import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
// import AdminLogin from '../views/admin/AdminLogin.vue'
// import store from '../store'
import cookies from 'vue-cookies'
// import md5 from 'js-md5'
Vue.use(VueRouter)
const adminRoutes = {
  path: '/admin',
  name: 'Admin',
  component: () => import('../views/admin/Admin.vue'),
  redirect: '/admin/home',
  children: [
    {
      path: '/admin/home',
      name: 'AdminHome',
      component: () => import('../views/admin/Home.vue'),
    },
    {
      path: '/admin/student',
      name: 'AdminStudent',
      component: () => import('../views/admin/AdminStudent.vue'),
    },
    {
      path: '/admin/question',
      name: 'AdminQuestion',
      component: () => import('../views/admin/AdminQuestion.vue'),
    },
    {
      path: '/admin/search',
      name: 'AdminSearch',
      component: () => import('../views/admin/AdminSearch.vue'),
    },
    {
      path: '/admin/search/:sid',
      name: 'AdminStudentSearch',
      component: () => import('../views/admin/AdminStudentSearch.vue'),
    },
    {
      path: '/admin/errorhandling',
      name: 'AdminCrash',
      component: () => import('../views/admin/AdminCrash.vue'),
    },
    {
      path: '/admin/maintain',
      name: 'AdminMaintain',
      component: () => import('../views/admin/AdminMaintain.vue'),
    },
    {
      path: '/admin/score',
      name: 'AdminScore',
      component: () => import('../views/admin/AdminScore.vue'),
    },
  ]
}
const routes = [

  {
    path: '/adminLogin',
    name: 'AdminLogin',
    component: () => import('../views/admin/AdminLogin.vue'),
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },


  {
    path: '/perview',
    name: 'Perview',
    component: () => import('../views/Perview.vue')
  },
  // {
  //   path: "/AdminPerview",
  //   name: 'AdminPerview',
  //   component: () => import('../views/admin/AdminPerview.vue')

  // },
  {
    path: "/401",
    name: '401',
    component: () => import('../views/error/401.vue')

  },
  {
    path: "/404",
    name: '404',
    component: () => import('../views/error/404.vue')

  }
]

// if (localStorage.getItem('yunkepwd') === md5('11-308-yunke')) {
//   routes.push(
//     {
//       path: "/AdminPerview",
//       name: 'AdminPerview',
//       component: () => import(/* webpackChunkName: "about" */ '../views/admin/AdminPerview.vue')

//     }
//   )
// }




const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

const staticRoutes = ['/404', '/401']
let flag = false;
router.beforeEach((to, from, next) => {
  // ...
  // 返回 false 以取消导航
  const token = cookies.get('token')
  // console.log(token);
  // // console.log(token);
  // console.log(from);
  console.log(router);
  console.log(cookies.get("admin"));
  if (cookies.get("admin") == 1 && !flag) {
    console.log(router);
    // cookies.set("admin",0)
    flag = true
    router.options.routes.push(adminRoutes)
    router.addRoute(adminRoutes)
    console.log("绑定");
    console.log(router);
    return next(to.path)
  }
  if (to.path == '/adminLogin') {
    return next()
  }
  // 考生拦截
  if (staticRoutes.indexOf(to.path) != -1) {
    console.log("---通过----");
    return next();
  }
  // 不存在重定向
  if (to.matched.length === 0) {  // 如果未匹配到路由
    return from.name ? next({ name: from.name }) : next('/404')
  }
  // const pwd = localStorage.getItem('yunkepwd')
  // if (to.path == '/AdminPerview' && pwd) {
  //   cookies.set('token', pwd)
  //   return next()
  // }
  // 考生路由
  if (token) {
    if (to.path == '/') {
      if (cookies.get('admin') == 1) {
        return next('/admin/home')
      }
      return next("/home")
    } else {
      return next();
    }
  } else if (to.path != '/') {
    return next('/')
  } else {
    next()
  }

})

export default router
